define(['mui', 'ajax','poppicker', 'citydata'], function(require) {
	mui.init({
		beforeback: function() {
			//释放内存
			headerImg.src = '';
			token = null;
			user = null;
			userdata = null;
			transtime = null;
			ms = null;
			h = null;
			//返回true，继续页面关闭逻辑
			return true;
		}
	});
	var token = null;
	var user = null;
	var userdata = null;
	var transtime = getNowFormatDate();
	var ms = null;
	var h = null;
	var headerImg = document.getElementById("headerImg");
	var con_name = document.getElementById("con_name")
	mui.plusReady(function() {
		console.log('log');
		user = judgeLogin();
		console.log('%o', user)
		h = plus.webview.getWebviewById(plus.runtime.appid); //获取应用入口页面的方法
		//更新个人消息；
		updateUserInfo(user);
		token = user.token;
		ms = plus.webview.getWebviewById('pages/user.html');
		if(user.headimg) {
			headerImg.src = main_host + user.headimg;
		}
		//修改个人信息
		document.getElementById("configUser").addEventListener('tap', function() {
			var companyName = document.getElementById("con_company").value;
			var address = document.getElementById("con_address").innerHTML;
			var id = con_name.value;
			//		if(id!=''){
			//			if((id.length<6||id.length>8)){
			//				mui.toast('登录名长度需要在6到8位之间');
			//				return;
			//			}
			//			var Regx = /^[A-Za-z]$/;
			//			if(!Regx.test(id[0])){
			//				mui.toast('登录名必须以字母开头');
			//				return;
			//			}
			//		}
			if(companyName == '') {
				companyName = user.company;
			}
			mui.ajax(ajax_modMemberURL, {
				data: {
					MemberType: 1,
					token: token,
					transtime: transtime,
					CompanyName: companyName,
					Address: address,
					UserName: id,
					//id:id,
				},
				dataType: 'json',
				type: 'post',
				timeout: _timeout,
				success: function(data) {
					data = JSON.parse(data);
					mui.toast(data.message);
					if(data.result == 1) {
						console.log('修改个人信息成功');
						document.getElementById("con_address").innerHTML = address;
						document.getElementById("con_company").setAttribute('placeholder', companyName);
						mui.fire(ms, 'getUserInfoFromServer', {

						});
					}
				},
				error: function(xhr, type, errorThrown) {
					//异常处理；
					console.log(JSON.stringify(xhr));
					mui.toast("请求出错");
				}
			});
		});
		//头像上传选择
		mui('body').on('tap', '.userAvatar', function() {
			require(['appupload', 'lrz'], function() {
				appupload.init(function(data) {
					lrz(data.dst, {
						width: 500,
						done: function(rs) {
							var url = ajax_uploadHeadImgURl;
							plus.nativeUI.showWaiting('', {
								padlock: true
							});
							mui.ajax(url, {
								data: {
									token: token,
									transtime: transtime,
									Img: rs.base64
								},
								dataType: 'json',
								type: 'post',
								timeout: _timeout,
								success: function(data) {
									plus.nativeUI.closeWaiting();
									console.log("information:", data)
									data = JSON.parse(data);
									if(data.result == 1) {
										document.getElementById("headerImg").src = main_host + data.url
										//保存信息
										user.headimg = data.url;
										var memberdata = JSON.stringify(user);
										plus.storage.setItem('memberdata', user);
										mui.fire(h, 'changeHeadImgIndex', {

										});
										mui.toast('上传成功');
									}

								},
								error: function(xhr, type, errorThrown) {
									//异常处理；
									console.log(xhr);
									console.log(type);
									console.log(errorThrown);
									plus.nativeUI.closeWaiting();
								}
							});
						}
					});

				});
			})

		});

		function updateUserInfo(data) {
			if(data.name != undefined) {
				con_name.value = data.name
				//con_name.setAttribute('disabled',"true")
			}
			if(data.company) {
				document.getElementById("con_company").setAttribute('placeholder', data.company);
			} else {
				document.getElementById("con_company").innerHTML = '';
			}
			if(data.starttime) {
				document.getElementById("con_startime").innerHTML = data.starttime;
			} else {
				document.getElementById("con_startime").innerHTML = '';
			}
			if(data.endtime) {
				document.getElementById("con_endtime").innerHTML = data.endtime;
			} else {
				document.getElementById("con_endtime").innerHTML = '';
			}
			if(data.address) {
				document.getElementById("con_address").innerHTML = data.address;
			} else {
				document.getElementById("con_address").innerHTML = '点击选择城市';
			}
			if(data.headimg) {
				document.getElementById("headerImg").src = main_host + data.headimg;
			}
			//		if(data.alias){
			//			document.getElementById("avatarEdit").innerHTML =data.alias;
			//		}
		}

	});
	//选择所在地

		(function($, doc) {
			$.ready(function() {
				//级联示例
				var cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData3);
				var showCityPickerButton = doc.getElementById('li_address');
				//var cityResult3 = doc.getElementById('cityResult3');
				var resultspan = doc.getElementById('con_address');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						resultspan.innerText = (items[0] || {}).text + " " + (items[1] || {}).text + " " + (items[2] || {}).text;
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			});
		})(mui, document);
	

})